package jp.co.technotree.common.controll;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import jp.co.technotree.common.util.DBGLog;


public class ApplicationController {
	private static String CLASSNAME=ApplicationController.class.getName();
	private SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
	private static long m_taskno = 0;

//	private RFIDManager rfidManager = null;

	public ApplicationController(){
		m_taskno=0;
	}


	/**
	 * フレームワーク本体
	 * @param req
	 * @param res
	 * @throws Exception
	 */
	
	public void init(ServletConfig config) throws ServletException {
		String METHODNAME = "init";
		DBGLog.fwdebug(CLASSNAME,METHODNAME,"======= Application START =======");

		ServletContext context = config.getServletContext();
		String path = context.getRealPath("/");
		
		//初期化
//		ApplicationContext.init(path);

		DBGLog.fwdebug(CLASSNAME,METHODNAME,"END");
	}

	/**
	 * タスクコード作成[YYYMDHMMSS-NO]
	 * YYY	: 年36進数
	 * M	: 月36進数
	 * D	: 日36進数
	 * H	: 時間36進数
	 * MM	: 分36進数
	 * SS	: 秒36進数
	 * NO	: 番号36進数
	 * @return
	 */
	private synchronized String getTaskCd(){
		Calendar cal = Calendar.getInstance();
	    int year = cal.get(Calendar.YEAR);        //(2)現在の年を取得
	    int month = cal.get(Calendar.MONTH) + 1;  //(3)現在の月を取得
	    int day = cal.get(Calendar.DATE);         //(4)現在の日を取得
	    int hour = cal.get(Calendar.HOUR_OF_DAY); //(5)現在の時を取得
	    int min = cal.get(Calendar.MINUTE); 	  //(5)現在の時を取得
	    int sec = cal.get(Calendar.SECOND); 	  //(5)現在の時を取得
	    String h = Long.toString(hour,36).toUpperCase();
	    String m = "00"+Long.toString(min,36).toUpperCase();
	    String s = "00"+Long.toString(sec,36).toUpperCase();
	    m = m.substring(m.length()-2,m.length());
	    s = s.substring(s.length()-2,s.length());

	    m_taskno = (m_taskno+1)%1000000;
		String strCd = "0000"+Long.toString(m_taskno,36).toUpperCase();
		strCd = strCd.substring(strCd.length()-4,strCd.length());
		strCd = Long.toString(year,36).toUpperCase()
					+Long.toString(month,36).toUpperCase()
					+Long.toString(day,36).toUpperCase()
					+h+m+s
					+"-"+strCd;

		return strCd;
	}


}
